<?php

namespace App\Http\Controllers\Account;

use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Account\UserPermission;
use App\Helpers\Code;

class UserPermissionController extends Controller
{

    public function __construct()
    {
        $this->rules = [
            'title' => 'bail|required|unique:user_permissions|max:50',
            'group_id' => 'bail|required|numeric',
            'description' => 'max:100',
            'slug' => 'bail|required|unique:user_permissions|max:50',
            'http_method' => 'bail|required|max:25',
            'http_path' => 'bail|required|max:255',
            'is_enabled' => 'boolean'
        ];

        $this->messages = [
            'title.required' => trans('permission.title_required'),
            'title.unique' => trans('permission.title_unique'),
            'title.max' => trans('permission.title_max'),
            'group_id.required' => trans('permission.group_id_required'),
            'group_id.numeric' => trans('permission.group_id_numeric'),
            'description.max' => trans('permission.description_max'),
            'slug.required' => trans('permission.slug_required'),
            'slug.max' => trans('permission.slug_max'),
            'slug.unique' => trans('permission.slug_unique'),
            'http_method.required' => trans('permission.http_method_required'),
            'http_method.max' => trans('permission.http_method_max'),
            'http_path.required' => trans('permission.http_path_required'),
            'http_path.max' => trans('permission.http_path_max'),
            'is_enabled.boolean' => trans('permission.is_enabled_boolean'),
        ];

        $this->model = new UserPermission();
    }

    public function addPermission(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addPermission($data);

            $this->checked($model, [Code::INSIDE_EXIST_DEPENDENCE => 'common.add_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function deletePermission($id)
    {
        try {
            $model = $this->model->deletePermission($id);

            $this->checked($model, ['null' => 'common.delete_failure', Code::INSIDE_EXIST_DEPENDENCE => 'permission.delete_failure_dependence']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function editPermission($id, Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['data_scope']));

            $this->rules['title'] = ['bail', 'required', 'max:50', \Illuminate\Validation\Rule::unique('user_permissions')->ignore($id)];
            $this->rules['slug'] = ['bail', 'required', 'max:50', \Illuminate\Validation\Rule::unique('user_permissions')->ignore($id)];

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editPermission($id, $data);

            $this->checked($model, [Code::INSIDE_EXIST_DEPENDENCE => 'common.edit_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getPermissionList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int) $request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            return preprocess($this->model->getPermissionList($limit, $sorter, $search));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getPermission($id)
    {
        try {
            $model = $this->model->getPermission($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

}
